import Vue from 'vue'
function formatDate (timestamp, format) {
  format = format || 'YYYY-MM-DD HH:mm:ss'
  if (!timestamp || timestamp === '') {
    return Vue.prototype.$moment(new Date()).format(format)
  } else {
    return Vue.prototype.$moment(timestamp).format(format)
  }
}

export const lineOptions = (xAxis, datas, title = "", unit = "%") => {
  return {
    title: {
      text: title
    },
    tooltip: {
      trigger: 'axis',
      formatter: (params) => {
        let res = `${params[0].axisValue}<br/>`
        params.forEach(item => {
          const name = item.seriesName
          res += `${name} : ${item.data} ${unit}<br/>`
        })
        return res
      },
      axisPointer: {
        animation: false
      }
    },
    legend: {
      data: datas?.map(v => v.name) || []
    },
    grid: {
      left: '3%',
      right: '4%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: xAxis || [],
      axisLabel: {
        rotate: 45,
        formatter: function (val) {
          return val
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        formatter: '{value} '+unit
      }
    },
    // series: [
    //   {
    //     name: 'Email',
    //     type: 'line',
    //     // stack: 'Total',
    //     data: [120, 182, 101]
    //   },
    //   {
    //     name: 'Union Ads',
    //     type: 'line',
    //     // stack: 'Total',
    //     data: [220, 132, 191]
    //   },
    // ]
    series: datas?.map(v => {
      return {
        name: v.name,
        type: 'line',
        // 设置线条颜色
        lineStyle: {
          color: '#67C23A',
          width: 2,
          type: 'solid'
        },
        // stack: 'Total',
        data: v.data
      }
    }) || []
  }
}